home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / progress.py < prev    next >
Encoding:
Python Source  |  2009-03-30  |  2.5 KB  |  106 lines

  1. import sys
  2. import time
  3.  
  4. import apt
  5.  
  6.  
  7. class TextProgress(apt.OpProgress):
  8.  
  9.     def __init__(self):
  10.         self.last=0.0
  11.  
  12.     def update(self, percent):
  13.         if (self.last + 1.0) <= percent:
  14.             sys.stdout.write("\rProgress: %i.2          " % (percent))
  15.             self.last = percent
  16.         if percent >= 100:
  17.             self.last = 0.0
  18.  
  19.     def done(self):
  20.         self.last = 0.0
  21.         print "\rDone                      "
  22.  
  23.  
  24. class TextFetchProgress(apt.FetchProgress):
  25.  
  26.     def __init__(self):
  27.         pass
  28.  
  29.     def start(self):
  30.         pass
  31.  
  32.     def stop(self):
  33.         pass
  34.  
  35.     def updateStatus(self, uri, descr, shortDescr, status):
  36.         print "UpdateStatus: '%s' '%s' '%s' '%i'" % (
  37.             uri, descr, shortDescr, status)
  38.  
  39.     def pulse(self):
  40.         print "Pulse: CPS: %s/s; Bytes: %s/%s; Item: %s/%s" % (
  41.             apt.SizeToStr(self.currentCPS), SizeToStr(self.currentBytes),
  42.             apt.SizeToStr(self.totalBytes), self.currentItems, self.totalItems)
  43.         return True
  44.  
  45.     def mediaChange(self, medium, drive):
  46.         print "Please insert medium %s in drive %s" % (medium, drive)
  47.         sys.stdin.readline()
  48.         #return False
  49.  
  50.  
  51. class TextInstallProgress(apt.InstallProgress):
  52.  
  53.     def __init__(self):
  54.         apt.InstallProgress.__init__(self)
  55.         pass
  56.  
  57.     def startUpdate(self):
  58.         print "StartUpdate"
  59.  
  60.     def finishUpdate(self):
  61.         print "FinishUpdate"
  62.  
  63.     def statusChange(self, pkg, percent, status):
  64.         print "[%s] %s: %s" % (percent, pkg, status)
  65.  
  66.     def updateInterface(self):
  67.         apt.InstallProgress.updateInterface(self)
  68.         # usefull to e.g. redraw a GUI
  69.         time.sleep(0.1)
  70.  
  71.  
  72. class TextCdromProgress(apt.CdromProgress):
  73.  
  74.     def __init__(self):
  75.         pass
  76.  
  77.     # update is called regularly so that the gui can be redrawn
  78.  
  79.     def update(self, text, step):
  80.         # check if we actually have some text to display
  81.         if text != "":
  82.             print "Update: %s %s" % (string.strip(text), step)
  83.  
  84.     def askCdromName(self):
  85.         print "Please enter cd-name: ",
  86.         cd_name = sys.stdin.readline()
  87.         return (True, string.strip(cd_name))
  88.  
  89.     def changeCdrom(self):
  90.         print "Please insert cdrom and press <ENTER>"
  91.         answer = sys.stdin.readline()
  92.         return True
  93.  
  94.  
  95. if __name__ == "__main__":
  96.     c = apt.Cache()
  97.     pkg = c["3dchess"]
  98.     if pkg.isInstalled:
  99.         pkg.markDelete()
  100.     else:
  101.         pkg.markInstall()
  102.  
  103.     res = c.commit(TextFetchProgress(), TextInstallProgress())
  104.  
  105.     print res
  106.